package com.gitee.wsl.struct.event

import com.gitee.wsl.reflect.Inject
import kotlin.test.Test


class CoroutineEventTest {

    fun EventsHandler.blockEvents(
        engine: Engine,
        block: () -> DesignBlock
    ) {
        val block: DesignBlock by Inject(block)

        register<BlockEvent.OnDelete> { engine.delete(block) }

        register<BlockEvent.OnBackward> { engine.sendBackward(block) }

    }

    interface BlockEvent:Event{
        class OnDelete: BlockEvent

        object OnBackward:BlockEvent
    }

    interface Engine{
        fun delete(block: DesignBlock)
        fun sendBackward(block: DesignBlock)
    }

    interface DesignBlock{}

    @Test
    fun testEventManager(){
        println("test on event manager start")
        val testEventManager = object : EventManager {

            init {
                eventHandler.blockEvents(
                    engine = object:Engine{
                    override fun delete(block: DesignBlock) {
                        println("call on delete")
                    }

                    override fun sendBackward(block: DesignBlock) {
                        println("call on sendBackward")
                    }
                                          },
                    block = {object:DesignBlock{} })
            }
        }
       /* testEventManager.eventHandler.forEach{
            println("${it.key} to ${it.value}")
        }*/

        testEventManager.fireEvent(BlockEvent.OnDelete())
    }

    @Test
    fun testEventManagerRegister(){
        println("test on event manager start")
        val engine = object:Engine{
                override fun delete(block: DesignBlock) {
                    println("call on delete")
                }

                override fun sendBackward(block: DesignBlock) {
                    println("call on sendBackward")
                }
            }
        val testEventManager = object : EventManager {
            init {
                registerEvent<BlockEvent.OnDelete> { engine.delete(object:DesignBlock{}) }
            }
        }

        /*testEventManager.eventHandler.forEach{
            println("${it.key} to ${it.value}")
        }*/

        testEventManager.fireEvent(BlockEvent.OnDelete())
    }
}